package com.ysd.lis.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.lis.aspect.SysEventLog;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.mapper.sys.SysEventLogMapper;
import com.ysd.lis.request.EventLogPageParam;
import com.ysd.lis.service.SysEventLogService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2023-11-06
 */
@Service
public class SysEventLogServiceImpl extends ServiceImpl<SysEventLogMapper, SysEventLog> implements SysEventLogService {
    @Autowired
    SysEventLogMapper sysEventLogMapper;

    public Result getPageLogging(EventLogPageParam eventLogPageParam) {
        Map returnMap = new HashMap<>();
        LambdaQueryWrapper<SysEventLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SysEventLog> page = new Page<SysEventLog>();
        page.setCurrent(eventLogPageParam.getPageIndex());
        page.setSize(eventLogPageParam.getPageSize());
        // lambdaQueryWrapper.eq(SysModule::getDelFlag, 0);
        SysUser sysUser = RedisUserManager.getUser();
        if (StringUtils.isNotBlank(eventLogPageParam.getOrgId())) {
            lambdaQueryWrapper.eq(SysEventLog::getOrgId, eventLogPageParam.getOrgId());
        }
        if (StringUtils.isNotBlank(eventLogPageParam.getUserName())) {
            lambdaQueryWrapper.eq(SysEventLog::getUserName, eventLogPageParam.getUserName());
        }
        if (StringUtils.isNotBlank(eventLogPageParam.getServiceName())) {
            lambdaQueryWrapper.eq(SysEventLog::getServiceName, eventLogPageParam.getServiceName());
        }
        if (StringUtils.isNotBlank(eventLogPageParam.getRequestUrl())) {
            lambdaQueryWrapper.like(SysEventLog::getRequestUrl, eventLogPageParam.getRequestUrl());
        }
        if (StringUtils.isNotBlank(eventLogPageParam.getBusinessName())) {
            lambdaQueryWrapper.like(SysEventLog::getBusinessName, eventLogPageParam.getBusinessName());
        }

        if (ToolsUtils.isNotEmpty(eventLogPageParam.getBeginLog()) && ToolsUtils.isNotEmpty(eventLogPageParam.getEndLog())) {
            lambdaQueryWrapper.ge(SysEventLog::getOperateTime, new Timestamp(DateUtil.parse(eventLogPageParam.getBeginLog()).getTime()))
                    .le(SysEventLog::getOperateTime, new Timestamp(DateUtil.parse(eventLogPageParam.getEndLog()).getTime()));
        }
        if (ToolsUtils.isNotEmpty(eventLogPageParam.getMachineIp())) {
            lambdaQueryWrapper.eq(SysEventLog::getMachineIp, eventLogPageParam.getMachineIp());
        }
        lambdaQueryWrapper.orderByDesc(SysEventLog::getCreateTime);
        lambdaQueryWrapper.select(SysEventLog::getId,
                SysEventLog::getOperateTime,
                SysEventLog::getCreateTime,
                SysEventLog::getHospitalName,
                SysEventLog::getModule,
                SysEventLog::getOperationType,
                SysEventLog::getServiceName,
                SysEventLog::getUserName,
                SysEventLog::getHospitalCode,
                SysEventLog::getMachineIp,
                SysEventLog::getMachineName,
                SysEventLog::getContent,
                SysEventLog::getRequestMethod,
                SysEventLog::getRequestUrl,
                SysEventLog::getMethod,
                SysEventLog::getTakeTime,
                SysEventLog::getBusinessName
        );
        Page<SysEventLog> logPageList = sysEventLogMapper.selectPage(page, lambdaQueryWrapper);
        returnMap.put("logPageList", logPageList);
        return Result.succ(1, "获取信息成功", returnMap);
    }
}
